//引入API
import { getAdminGoods, getAdminCategory, getAdminBrand, postPutimg, postAddGoods } from "@/request/api.js";
//引入路由
import router from '@/router';
//引入message-element
import {Message} from 'element-ui'

export default {
    state: {
        pageSize: 10,
        tableData: [{}],
        count: 100,
        currentPage: 1,
        // 分类数组
        categoryList: [{}],
        brandList: [{}],     // 品牌数组
        form: {},            //存放图片地址
        gallery:[],
        subsign: {
            list_pic_url: '',
            primary_pic_url: '',
            category_id: '',
            type: [],
            gallery:[],
            gallerys:[],
            attribute:[],
            name: '',
            brand_id: '',
            goods_brief: '',
            goods_unit: '',
            goods_number: '',
            retail_price: '',
            is_new: false,
            is_hot: false,
            is_delete: false,
            sort_order: '',
            goods_desc:'',
        }          //请求数据

    },
    mutations: {
        //删除图片时对应vuex的修改
        goodschangegallerys(state,payload){
           let index= state.subsign.gallerys.indexOf(payload)//下标
           console.log(index)
           state.subsign.gallerys.splice(index,1)
           console.log(state.subsign.gallerys)
        },
        //编辑时获取传过来的值
        goodseditsubsign(state, payload) {
            // console.log(payload)
            // console.log('编辑前的参数',state.subsign)

            state.subsign={...payload}
            //对值做相应调整
            if (payload.is_new) {
                state.subsign.is_new = true
            } else {
               state.subsign.is_new = false
            }
            if (payload.is_hot) {
               state.subsign.is_hot = true
            } else {
                state.subsign.is_hot = false
            }
            if (payload.is_delete) {
                state.subsign.is_delete = false
            } else {
                state.subsign.is_delete = true
            }
            //遍历轮播图数组

            state.subsign.gallery=[]
            state.subsign.gallerys=[]

            payload.gallery.map(val=>{

                state.subsign.gallery.push({'url':val.img_url})

                state.subsign.gallerys.unshift(val.img_url)
            })
            // console.log(state.subsign.gallery)
        },
        //初始化参数
        goodscreatedsubsign(state, payload) {
            state.subsign = { ...payload }
        },
        goodsAddattribute(state, payload) {
            state.subsign.attribute.push({ attribute_id: '', value: '' })
            // console.log(state.subsign.attribute)
        },
        goodsDeleteattribute(state, payload) {
            state.subsign.attribute.splice(payload, 1)
            // console.log(state.subsign.attribute)

        },
        changetableData(state, payload) {
            state.tableData = payload.data
            state.count = payload.count
            state.currentPage = payload.currentPage
            state.pageSize = payload.pageSize
        },
        //获取值
        goodsGetvalue(state, payload) {
            // state.subsign={...payload}

            state.subsign = payload

        }
    },
    actions: {
        changetableDatas({ commit, rootState }, payload = payload || {}) {
            // console.log(payload)
            getAdminGoods({
                page: payload.currnet || rootState.goods.currentPage,
                size: payload.size || rootState.goods.pageSize
            }).then(res => {
                // console.log(res);
                commit('changetableData', res.data)
            });
        },
        goodsaddput({ commit, state }) {
            getAdminCategory({ size: 100 }).then(res => {
                // console.log("分类数组",res.data)
                state.categoryList = res.data;
                // console.log( state.categoryList)
                getAdminBrand({ size: 100 }).then(resq => {
                    // console.log("品牌数组",resq.data.data)
                    state.brandList = resq.data.data;
                    // console.log(state.brandList)
                })

            })
        },
        goodsPutimg({ commit, state }, payload) {
            console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                console.log(res.data)
                // state.form[res.data.name]=res.data.fileUrl
                state.subsign.list_pic_url = res.data.fileUrl
                state.subsign.primary_pic_url = res.data.fileUrl
                // console.log(state.form)
            })
        },
        //轮播图
        goodsPutimgs({ commit, state }, payload) {
            // console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                state.subsign.gallerys.unshift(res.data.fileUrl)
                // state.subsign.gallery.push({'url':res.data.fileUrl})
            })
        },
        goodsAddgoods({ commit, state }) {
            //请求前换值
            let is_new=0;
            let is_hot=0;
            let is_delete=0;
            if (state.subsign.is_new) {
                is_new = 1
            } else {
               is_new = 0
            }
            if (state.subsign.is_hot) {
               is_hot = 1
            } else {
                is_hot = 0
            }
            if (state.subsign.is_delete) {
                is_delete = 0
            } else {
                is_delete = 1
            }
            // console.log("gallerys是否存在?",state.subsign.gallerys)
            postAddGoods({
                ...state.subsign,
                is_new,
                is_hot,
                is_delete
            }).then(res => {
                if(state.subsign.id){
                    Message({
                        message:'修改成功',
                        type:'success'
                    })
                }else{
                    Message({
                        message:'添加成功',
                        type:'success'
                    })
                }

                router.push('/good')
                if (res) {
                    console.log(res)
                } else {
                    console.log('请求失败')
                }
            })
        }
    }
}